Requires Scripting PRO
The Translation API enables text translation between different languages. It supports both individual and batch translation use cases and is available on iOS 18.0 or later.
The API is exposed as a Translation class, which includes:
TranslationTranslation.shared: TranslationProvides a shared singleton instance of the Translation class. This is useful in scripts that do not have a UI or need to reuse a common translation host.
translate(options): Promise<string>Translates a single string from a source language to a target language.
options.text: string
The input string to be translated.
options.source?: string
The source language code (e.g., "en" for English). If omitted or null, the translation system will attempt to detect the source language automatically. If detection is ambiguous, the user may be prompted to clarify.
options.target?: string
The target language code (e.g., "es" for Spanish). If omitted or null, the system will choose an appropriate target language based on the device’s Device.preferredLanguages and the detected source language.
Promise<string> — A promise that resolves to the translated string.translateBatch(options): Promise<string[]>Translates an array of strings from a source language to a target language.
options.texts: string[]
An array of strings to translate. The order of translations in the result matches the input array.
options.source?: string
The source language code. Behaves the same as in the translate method.
options.target?: string
The target language code. Behaves the same as in the translate method.
Promise<string[]> — A promise that resolves to an array of translated strings.Language codes should follow ISO 639-1 format (e.g., "en", "zh", "de", "fr").
This API leverages system-level translation capabilities and may prompt for language disambiguation in certain cases.
Use the translationHost view modifier in the following scenarios:
Interactive Translations in UI Views
When your script presents a user interface (e.g., using <VStack>, <List>, etc.) and performs translations using a custom Translation instance (created via new Translation()), you must apply translationHost to the root view to allow the system to display permission dialogs, language download prompts, or source language selection alerts.
Source Language Is null
If you omit the source field in your translation request and rely on the system to detect the language, translationHost ensures the system can prompt the user if detection fails.
Languages May Need Downloading
If the device does not have the required source or target language installed, translationHost enables system prompts to download those languages interactively.
You do not need to set translationHost when using the pre-bound Translation.shared instance in a headless or background script that does not render a user interface.